var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var root = ui("$");
var listdata = mm("do_ListData");
var openHelper = require("helper/open");
//andriod返回键
do_Page.on("back", function() {
  do_App.closePage();
});
var noData = [ {
	"template" : 1
} ];

var headerAout = ui("headerAout");
var listview = ui("yellowList");
var qqHttp = require("helper/http");
var communityUrl = require("url/community");
var communityUtil = require("helper/community");
var textFieldSearch=ui("do_TextField_Search")
var listProductData = mm("do_ListData");
var nolistdata = mm("do_ListData");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
  do_App.closePage()
});
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text = "社区黄页";

//当前页码
var page = 1,name="",address="",phone="";
// 本页面-ajax业务请求url
var httpUrl = communityUrl.url.yellow.page;

var yellowList = ui("yellowList");
var datas = [{"name":"圈圈生活","phone":"0755-12345678"},{"name":"桃源居物业","phone":"0755-12345678"}];
yellowList.items = datas;

//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	binddata();
});
var type;
var search;
//邦定数据源
listview.bindItems(listdata);
//全部刷新数据
function binddata() {
	page = 1;
	var params = {
		page : page,
		search : search,
		type : type,
		communityId : communityUtil.communityVisitId()
	}
	qqHttp.post(httpUrl, params, function(data) {
		listview.rebound();
		listdata.removeAll();// 清除数据
		if (data.page) {
			if(data.page.content.length){
				listdata.addData(data.page.content);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}

function getNextPageData() {
	page++;
	var params = {
		page : page,
		search : search,
		type : type,
		communityId : communityUtil.communityVisitId()
	}
	qqHttp.post(httpUrl, params, function(data) {
		if (data.page) {
			listview.rebound();
			listdata.addData(data.page.content);
			listview.refreshItems();
		}
	});
}

yellowList.on("touch", function(data, e) {
	var ondata = listdata.getOne(data);
	openHelper.open("source://view/community/yellow/detail.ui",{
		yellowId:ondata.id
	});
});
var params={};

textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});
function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	binddata();
}

//点击便民
ui("do_Convenience").on("touch", function(data){
	type=ui("do_Convenience").tag;
	binddata();
});
//点击家政
ui("do_Homemaking").on("touch", function(data){
	type=ui("do_Homemaking").tag;
	binddata();
});
//点击购物
ui("do_Shopping").on("touch", function(data){
	type=ui("do_Shopping").tag;
	binddata();
});
//点击维修
ui("do_Service").on("touch", function(data){
	type=ui("do_Service").tag;
	binddata();
});

//点击背景隐藏输入键盘
root.on("touch",function(){
	do_Page.hideKeyboard();
})